package com.antu.first.web.merchant;

import com.alibaba.fastjson.JSON;
import com.antu.first.bean.DiscountCoupon;
import com.antu.first.bean.Hotel;
import com.antu.first.bean.Merchant;
import com.antu.first.service.DiscountCouponService;
import com.antu.first.service.MerchantLogionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/discountCouponController",produces = "text/plain;charset=utf-8")
public class DiscountCouponController {
    @Autowired
    private DiscountCouponService discountCouponService;
    Map<String,Object> modelMap=new HashMap<String,Object>();
    int pageSize=10;//分页条数
    //优惠券添加
    @RequestMapping(value = "/addDiscountCoupon",method = RequestMethod.POST)
    @ResponseBody
    public String addDiscountCoupon(String dName,String dDesc,String dStart,String dStop,String dMinConsum,String dValue,String dCount,HttpServletRequest request){
        Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        DiscountCoupon discountCoupon=new DiscountCoupon();
        discountCoupon.setHotel(hotel);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1=sdf.parse(dStart);
            Date date2=sdf.parse(dStop);
            discountCoupon.setdStart(date1);
            discountCoupon.setdStop(date2);
        } catch (ParseException e) {
            modelMap.put("errMsg","时间转换错误，请稍后再试！");
            e.printStackTrace();
        }
        discountCoupon.setdName(dName);
        discountCoupon.setdDesc(dDesc);
        try{
            Double dou1=Double.parseDouble(dMinConsum);
            Double dou2=Double.parseDouble(dValue);
            Integer integer=Integer.parseInt(dCount);
            discountCoupon.setdMinConsum(dou1);
            discountCoupon.setdValue(dou2);
            discountCoupon.setdCount(integer);
        }catch (Exception e){
            modelMap.put("errMsg","数字转换错误，请稍后再试！");
            e.getMessage();
        }
        discountCoupon.setdCreator(m);
        discountCoupon.setHotel(hotel);
        discountCoupon.setdStatus(0);//设置优惠券状态默认值为0，未生效
        discountCoupon.setdCanModify(1);//设置优惠券默认为可修改
        Date date=new Date();
        discountCoupon.setdCreateTime(date);
        try {
            modelMap= discountCouponService.addDiscountCoupon(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","根据面值查询优惠券错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr= JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //根据面值查询优惠券
    @RequestMapping(value = "/selectDiscountCouponByValue",method = RequestMethod.POST)
    @ResponseBody
    public String selectDiscountCouponByValue(DiscountCoupon discountCoupon,HttpServletRequest request){
        //Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        //Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        //discountCoupon.setHotel(hotel);
        Hotel hotel=new Hotel();
        hotel.sethId(2);
        discountCoupon.setHotel(hotel);
        try {
            modelMap=discountCouponService.selectDiscountCouponByValue(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","根据面值查询优惠券错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //根据时间区间查询此区间内还可用的优惠券
    @RequestMapping(value = "/selectDiscountCouponByDate",method = RequestMethod.POST)
    @ResponseBody
    public String selectDiscountCouponByDate(DiscountCoupon discountCoupon,HttpServletRequest request){
        //Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        //Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        //discountCoupon.setHotel(hotel);
        Hotel hotel=new Hotel();
        hotel.sethId(2);
        discountCoupon.setHotel(hotel);
        try {
            modelMap= discountCouponService.selectDiscountCouponByDate(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","根据时间区间查询优惠券错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //商家查看未生效的优惠券
    @RequestMapping(value = "/selectDiscountCouponUnuse",method = RequestMethod.POST)
    @ResponseBody
    public String selectDiscountCouponUnuse(DiscountCoupon discountCoupon,HttpServletRequest request){
        //Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        //Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        //discountCoupon.setHotel(hotel);
        Hotel hotel=new Hotel();
        hotel.sethId(2);
        discountCoupon.setHotel(hotel);
        Date date=new Date();
        discountCoupon.setdStart(date);
        try {
            modelMap = discountCouponService.selectDiscountCouponDateByUnuse(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","未生效优惠券查询错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //商家查看生效中的优惠券
    @RequestMapping(value = "/selectDiscountCouponDateByUsing",method = RequestMethod.POST)
    @ResponseBody
    public String selectDiscountCouponDateByUsing(DiscountCoupon discountCoupon,HttpServletRequest request){
        //Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        //Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        //discountCoupon.setHotel(hotel);
        Hotel hotel=new Hotel();
        hotel.sethId(2);
        discountCoupon.setHotel(hotel);
        Date date=new Date();
        discountCoupon.setdStart(date);
        try {
            modelMap= discountCouponService.selectDiscountCouponDateByUsing(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","生效中优惠券查询错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }

    //商家查看未生效的优惠券
    @RequestMapping(value = "/selectDiscountCouponDateByUsed",method = RequestMethod.POST)
    @ResponseBody
    public String selectDiscountCouponDateByUsed(DiscountCoupon discountCoupon,HttpServletRequest request){
        //Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        //Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        //discountCoupon.setHotel(hotel);
        Hotel hotel=new Hotel();
        hotel.sethId(2);
        discountCoupon.setHotel(hotel);
        Date date=new Date();
        discountCoupon.setdStart(date);
        try {
            modelMap= discountCouponService.selectDiscountCouponDateByUsed(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","未生效优惠券查询错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //根据ID修改优惠券信息
    @RequestMapping(value = "/updateDiscountCouponInfor",method = RequestMethod.POST)
    @ResponseBody
    public String updateDiscountCouponInfor(DiscountCoupon discountCoupon,HttpServletRequest request){
        //Merchant m=(Merchant) request.getSession().getAttribute("merchant");
        //Hotel hotel=m.getHotel();//正常跑的时候，从session里取
        //discountCoupon.setHotel(hotel);
        //discountCoupon.setdCreator(m);
        try {
            modelMap=discountCouponService.updateDiscountCouponInfor(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","优惠券修改错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //根据ID删除优惠券
    @RequestMapping(value = "/deleteDiscountCouponById",method = RequestMethod.POST)
    @ResponseBody
    public String deleteDiscountCouponById(DiscountCoupon discountCoupon){
        try {
            modelMap=discountCouponService.deleteDiscountCouponById(discountCoupon);
        } catch (Exception e) {
            modelMap.put("errMsg","优惠券删除错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }
    //查询所有优惠券
    @RequestMapping(value = "/selectAllDiscountCoupon",method = RequestMethod.POST)
    @ResponseBody
    public String selectAllDiscountCoupon(HttpServletRequest request,Integer pageIndex){
        try {
            modelMap=discountCouponService.selectAllDiscountCoupon(request,pageIndex,pageSize);
        } catch (Exception e) {
            modelMap.put("errMsg","优惠券查询查错误，请稍后再试");
            e.printStackTrace();
        }
        String jsonStr=JSON.toJSONString(modelMap);
        return jsonStr;
    }

    /**
     * 根据优惠券id 查询优惠券信息
     * 王易山 加 2018-3-14
     */
    @RequestMapping(value = "/queryDiscountCouponById",method = RequestMethod.GET)
    @ResponseBody
    public String queryDiscountCouponById(Integer id){
        DiscountCoupon discountCoupon = new DiscountCoupon();
        discountCoupon = discountCouponService.queryDiscountCouponById(id);
        String str = JSON.toJSONString(discountCoupon);
        return str;
    }

}
